package com.eliza.springboot.v3.http.control.webFlux

import com.eliza.springboot.v3.model.Greeting
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.data.repository.query.Param
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import java.util.concurrent.atomic.AtomicLong


@RestController
@RequestMapping("/req")
class Req {
    private val counter = AtomicLong()

    @GetMapping("/RequestParam")
    fun greeting(@RequestParam(value = "name", defaultValue = "World") name: String): Greeting {
        return Greeting(counter.incrementAndGet(), name)
    }

    @GetMapping("/Param")
    fun param(@Param(value = "name") name: String): Greeting {
        return Greeting(counter.incrementAndGet(), name)
    }

    @GetMapping("/ModelAttribute")
    fun ModelAttribute(@ModelAttribute(value = "message") message: String): Greeting {
        return Greeting(counter.incrementAndGet(), message)
    }

    @GetMapping("/RequestAttribute")
    fun RequestAttribute(@RequestAttribute(value = "site") site: Any): Any {
        return site
    }

    @PostMapping("/Json")
    fun json(@RequestBody user: Any): Any {
        return user
    }

    @GetMapping("/Path/{month}")
    fun path(@PathVariable("month") month: String): Any {
        return month
    }

    @PostMapping("/Form")
    fun form(@RequestParam("files") files: Array<MultipartFile>): Any {
        return files
    }

    @PostMapping("/File/more")
    fun uploadFiles(files: Array<MultipartFile>): Any {
        return files
    }

    @PostMapping("/File/one")
    fun uploadFile(file: MultipartFile): String {
        return "${file.name} ${file.size}"
    }

    @PostMapping("/http")
    fun mete(
        request: HttpServletRequest,
        response: HttpServletResponse
    ): Any {
        response.status = 404

        return request.requestURI
    }
}